package com.zcoj.controller.User.contest;

import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.pojo.Dto.Contest;
import com.zcoj.pojo.Dto.ContestAnnouncement;
import com.zcoj.pojo.Vo.ContestRegisterVO;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.ContestAnnouncementService;
import com.zcoj.service.ContestRecordService;
import com.zcoj.service.ContestService;
import com.zcoj.util.RedisCaches;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 接口
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@RestController
@RequestMapping("/contest")
@Api(tags = "接口")
public class ContestController {

    @Autowired
    private ContestService contestService;

    @Autowired
    private RedisCaches redisCaches;

    @Autowired
    private ContestRecordService contestRecordService;

    @Autowired
    private ContestAnnouncementService contestAnnouncementService;

    /**
     * 分页查询
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public ApiResponse findPage (@RequestBody PageWrap<Contest> pageWrap) {
        return ApiResponse.success(contestService.findPage(pageWrap));
    }

    @PostMapping("/findPageVo")
    public ApiResponse findPageVo(@RequestBody PageWrap<Contest> pageWrap) {
        return ApiResponse.success(contestService.findPageVo(pageWrap));
    }

    @GetMapping("/checkIsRegister/{cid}")
    public ApiResponse checkIsRegister(@PathVariable Integer cid){
        return ApiResponse.success(contestService.checkIsRegister(cid));
    }

    @PostMapping("/submitRegister")
    public ApiResponse submitRegister(@RequestBody ContestRegisterVO contestRegisterVO){
        return ApiResponse.success(contestService.submitRegister(contestRegisterVO));
    }
    /**
     * 通过ID查询
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public ApiResponse finById(@PathVariable Integer id) {
        LoginUser loginUser = redisCaches.getLoginUser();
        Contest contest = contestService.findById(id);
        if(contest.getStatus()==0){
            if(!(loginUser.getPermission().contains("admin")||loginUser.getPermission().contains("super_admin")||loginUser.getPermission().contains("contest_admin"))){
               return ApiResponse.failed("比赛禁用");
            }else {
                return ApiResponse.success(contest);
            }
        }else {
            return ApiResponse.success(contest);
        }

    }


    @GetMapping("/getContestAnnouncementId/{cid}")
    public ApiResponse getContestAnnouncementId(@PathVariable Integer cid){
        ContestAnnouncement contestAnnouncement = new ContestAnnouncement();
        contestAnnouncement.setCid(cid);
        ContestAnnouncement one = contestAnnouncementService.findOne(contestAnnouncement);

        if( Objects.nonNull(one)){
            return ApiResponse.success(one.getAid());
        }else {
            return ApiResponse.success(0);
        }
    }


    @GetMapping("/getContestTime/{cid}")
    public ApiResponse getContestTime(@PathVariable Integer cid){
        return ApiResponse.success(contestService.getContestTime(cid));
    }


    @GetMapping("/getContestProblemID/{cid}")
    public ApiResponse getContestProblemID(@PathVariable Integer cid){
        return ApiResponse.success(contestRecordService.getContestProblemID(cid));
    }

    @GetMapping("/getContestIORank/{cid}")
    public ApiResponse getContestIORank(@PathVariable Integer cid){
        return ApiResponse.success(contestRecordService.contestIORank(cid));
    }

}
